<?php

class Ext_Article_Attachment_Shopping
{
    protected $_articleId;
    
    public function __construct($articleId)
    {
        if(!$articleId){
            require_once 'Ext/Article/Exception.php';
            throw new Ext_Article_Exception("Article id empty.");
        }
        
        $this->_articleId = $articleId;
    }
    
    public function add($videoDataId)
    {
        $_articleShoppingRow = App_Model_DbTable_Article_Attachment_Shopping::getInstance()->getRowByArticleId($this->_articleId);
        if(!$_articleShoppingRow){
            $row = array(
                'articleId' 	=> $this->_articleId,
                'ids' 	        => $videoDataId,
            );
            App_Model_DbTable_Article_Attachment_Shopping::getInstance()->addNew($row);
    		
        }else{
            $_ids = $_articleShoppingRow['ids'];
            $_ids .= ';'.$videoDataId;
            
            $row = array(
                'ids' 	=> $_ids,
            );
            App_Model_DbTable_Article_Attachment_Shopping::getInstance()->addOld($row, $this->_articleId);
        }
    }
    
    public function del($videoDataId)
    {
        $_articleShoppingRow = App_Model_DbTable_Article_Attachment_Shopping::getInstance()->getRowByArticleId($this->_articleId);
        if(!$_articleShoppingRow){
            return "";
        }
        
        $_ids = $_articleShoppingRow['ids'];
        
        $_newIdsArray = array();
        $_idsArray = explode(';', $_ids);
        for($i=0;$i<count($_idsArray);$i++)
        {
            $_id = $_idsArray[$i];
            if($_id == $videoDataId){
                continue;
            }
            
            if(in_array($_id, $_newIdsArray)){
                continue;
            }
            
            $_newIdsArray[] = $_id; 
        }
        
        $row = array(
            'ids' 	=> join(';', $_newIdsArray),
        );

        App_Model_DbTable_Article_Attachment_Shopping::getInstance()->addOld($row, $this->_articleId);
    }
}
